const app = getApp();
const imgBaseUrl = app.globalData.imgBaseUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBaseUrl: imgBaseUrl
  },
  bindInput:function(e){
    this.setData({
      money:e.detail.value
    })
  },
  bindPhone:function (e) {
    this.setData({
      phone:e.detail.value
    })
  },
  bindRecharge:function(){
    var money=this.data.money;
    var phone = this.data.phone;
    if(!money){
      wx.showToast({
        title: '请输入充值金额',
        icon:'none'
      })
      return;
    }
    if(money<=0){
      wx.showToast({
        title: '充值金额必须大于0！',
        icon: 'none'
      })
      return;
    }
    if(!phone){
      wx.showToast({
        title: '手机号码必须填写！',
        icon: 'none'
      })
      return;
    }
    if(!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机号码格式有误！',
        icon: 'none'
      })
      return;
    } 
    app.func.postReq('/text/index.php/home/yeuser/addye', { price: money, cphone: phone},function(res){
      wx.requestPayment({
        'timeStamp': res.timeStamp,
        'nonceStr': res.nonceStr,
        'package': res.package,
        'signType': 'MD5',
        'paySign': res.paySign,
        'success': function (res) {
          console.log(res, '支付成功');
          wx.showToast({
            title: '充值成功',
            icon: 'success'
          })
          var timer2=setTimeout(function () {
            clearTimeout(timer2);
            wx.navigateBack({

            })
          }, 1500)
        },
        'fail': function (res) {
          console.log(res, '支付失败');
          wx.showToast({
            title: '支付取消',
            icon: 'none'
          })
        }
      })
    })
  }
})